<?php

/**
 * +---------------------------------------------------------------
 * | Author: 北京永青宇创科技有限公司
 * | Date: 2020/03/12
 * | 版本: 6.0
 * +---------------------------------------------------------------
 * | CopyRight: 北京永青宇创科技有限公司 All rights reserved.
 * +---------------------------------------------------------------
 * | 版权声明：云豹不是一个自由软件，是云豹官方推出的商业源码。
 * | 严禁在未经许可的情况下拷贝、复制、传播、使用云豹的任意代码
 * | 如有违反，请立即删除，否则您将面临承担相应，法律责任的风险。
 * | 如果需要取得官方授权，请联系官方 https://www.gepardshop.com
 * +---------------------------------------------------------------
 */

namespace app\mall\logic;

use app\common\logic\LogicBase;
use \yunbao\yunbao;
use think\Db;
use \yunbao\helpers\StringHelper;

class Order extends LogicBase {
	public function getOrder($data = [])
	{
		return self::getList($data);
	}

	/*
		创建订单逻辑
		1、生成订单
		2、加入订单产品（暂时没有此功能）
		3、清空购物车（暂时没有此功能）
	*/
	public function createOrder($data = [])
	{
		// 设置 goods_price  商品总价
		if( ! isset($data['goods_price'])) {
			$data['goods_price'] = $data['order_amount'];
		}
		// 设置 total_amount  订单总价
		if( ! isset($data['total_amount'])) {
			$data['total_amount'] = $data['order_amount'];
		}
		// 订单数据验证
		$validate        = yunbao::appValidate($this->name);
		$validate_result = $validate->scene("add")->check($data);
		if( ! $validate_result) {
			return [40048, $validate->getError(), null];
		}
		// 设置 order_note  订单信息
		if( ! isset($data['order_note'])) {
			$data['order_note'] = "订单支付";
		}
		// 设置 支付code
		if( ! isset($data['pay_code'])) {
			if(request()->isMobile()) {
				$data['pay_code'] = "wap";
			} else {
				$data['pay_code'] = "pc";
			}
		}

		// 设置 支付名称
		switch($data['pay_handle']) {
			case 'alipay':
				$data['pay_name'] = "支付宝";
				break;
			case 'weixin':
				$data['pay_name'] = "微信";
				break;
			default:
				$data['pay_name'] = "其它";
				break;
		}

		//  需要验证的数据  goods_price  order_amount
		$order_sn         = StringHelper::get_order_sn("SN");
		$data['order_sn'] = $order_sn;
		Db::startTrans();
		try {

			$last_id = yunbao::loadAppModel($this->name)
			                 ->saveObject($data, ['order_sn', 'order_note', 'user_id', 'address', 'zipcode', 'goods_price', 'order_amount', 'total_amount', 'pay_handle', 'pay_name', 'pay_code', 'shipping_price', 'user_money', 'add_time']);
			if($last_id) {
				$order_goods = $data['order_goods'];
				$validate    = yunbao::appValidate("OrderGoods");
				foreach($order_goods as $key => $order_good) {
					// 设置 goods_price  商品总价
					if( ! isset($order_good['market_price'])) {
						$order_good['market_price'] = $order_good['goods_price'];
					}
					$order_good['order_id'] = $last_id;
					$validate_result        = $validate->scene("add")->check($order_good);
					if( ! $validate_result) {
						throw new \Exception($validate->getError());
					}
					$result = yunbao::loadAppModel("OrderGoods")->saveObject($order_good);
				}
				Db::commit();

				return [0, "订单生成成功", url('payment/order/pay', ['order_sn' => $order_sn])];
			} else {
				return [40048, "订单生成失败"];
			}
		} catch(\Exception $e) {
			Db::rollback();

			return [40048, "订单生成失败"];
		}
	}
}